package br.com.arosa.dao;

import java.util.*;
import java.sql.*;
import br.com.easynet.database.DataSet;
import br.com.jdragon.dao.*;
import br.com.arosa.transfer.*;

public class Tb_compraDAO extends ObjectDAO { 
	 public Tb_compraDAO(DAOFactory dao) throws Exception {
		 setDAOFactory(dao);
 		 con = dao.create();
 	 }

	 public void insert(Tb_compraT tb_compraT ) throws Exception { 
		 PreparedStatement pStmt=null;
		 try {
			 String sql = "insert into tb_compra  ( id_compra, id_fornecedor, id_forma_pagamento, data_compra, tipo_desconto, desconto) values ( ? , ? , ? , ? , ? , ? )";
			 pStmt = con.prepareStatement(sql);
			 pStmt.setObject(1, tb_compraT.getId_compra());
			 pStmt.setObject(2, tb_compraT.getId_fornecedor());
			 pStmt.setObject(3, tb_compraT.getId_forma_pagamento());
		 java.sql.Date dt4= new java.sql.Date(tb_compraT.getData_compra().getTime());
			 pStmt.setObject(4, dt4);
			 pStmt.setObject(5, tb_compraT.getTipo_desconto());
			 pStmt.setObject(6, tb_compraT.getDesconto());
			 pStmt.execute();
 		 } catch (Exception e) {
 			  throw e;
 		} finally {
 			 try { 
 			 pStmt.close();} catch (Exception e) {}
 			  
 		} 
 	} 

 	 public void update(Tb_compraT tb_compraT ) throws Exception { 
		 PreparedStatement pStmt=null;
		 try {
			 String sql = "update tb_compra set  id_fornecedor=?, id_forma_pagamento=?, data_compra=?, tipo_desconto=?, desconto=?  where  id_compra=?";
 			 pStmt = con.prepareStatement(sql);
			 pStmt.setObject(1, tb_compraT.getId_fornecedor());
			 pStmt.setObject(2, tb_compraT.getId_forma_pagamento());
		 java.sql.Date dt3= new java.sql.Date(tb_compraT.getData_compra().getTime());
			 pStmt.setObject(3, dt3);
			 pStmt.setObject(4, tb_compraT.getTipo_desconto());
			 pStmt.setObject(5, tb_compraT.getDesconto());
			 pStmt.setObject(6, tb_compraT.getId_compra());
			 pStmt.execute();
 		 } catch (Exception e) {
 			  throw e;
 		} finally {
 			 try { 
 			 pStmt.close();} catch (Exception e) {}
 			  
 		} 
 	} 

 	 public void delete(Tb_compraT tb_compraT ) throws Exception { 
		 PreparedStatement pStmt=null;
		 try {
			 String sql = "delete from tb_compra where  id_compra=?";
			 pStmt = con.prepareStatement(sql);
			 pStmt.setObject(1, tb_compraT.getId_compra());
			 pStmt.execute();
		 } catch (Exception e)  {
 			  throw e;
 		} finally {
 			 try { 
 			 pStmt.close();} catch (Exception e) {}
 			  
 		} 
 	} 

 	 private List<Tb_compraT> resultSetToObjectTransfer(ResultSet rs) throws Exception { 
		 List<Tb_compraT> objs = new Vector();
		 while (rs.next()) { 
 			 Tb_compraT tb_compraT = new Tb_compraT();
			 tb_compraT.setId_compra(rs.getInt("id_compra"));
			 tb_compraT.setId_fornecedor(rs.getInt("id_fornecedor"));
			 tb_compraT.setId_forma_pagamento(rs.getInt("id_forma_pagamento"));
			 tb_compraT.setData_compra(rs.getDate("data_compra"));
			 tb_compraT.setTipo_desconto(rs.getInt("tipo_desconto"));
			 tb_compraT.setDesconto(rs.getInt("desconto"));
			 objs.add(tb_compraT);
 		 }
 		 return objs; 
 	 }
	 public List<Tb_compraT> getAll() throws Exception { 
		 PreparedStatement pStmt=null;
		 ResultSet rs=null;
		 try {
			 String sql = "select * from tb_compra"; 
			 pStmt = con.prepareStatement(sql);
			 rs = pStmt.executeQuery();
 			 List<Tb_compraT> list = resultSetToObjectTransfer(rs);
 			 return list; 
 		 } catch (Exception e) {
 			  throw e;
 		} finally {
 			 try { rs.close();
 			 pStmt.close();} catch (Exception e) {}
 			  
 		} 
 	} 
	 
	 public Integer getNextCodigo() throws Exception { 
		 PreparedStatement pStmt=null;
		 ResultSet rs=null;
		 try {
			 String sql = "select coalesce(max(id_compra),0) as id_compra from tb_compra"; 
			 pStmt = con.prepareStatement(sql);
			 rs = pStmt.executeQuery();
 			 rs.next(); 			 
 			 return (rs.getInt("id_compra")+1); 
 		 } catch (Exception e) {
 			  throw e;
 		} finally {
 			 try { rs.close();
 			 pStmt.close();} catch (Exception e) {} 			  
 		} 
 	} 

 	 public List<Tb_compraT> getById(Tb_compraT tb_compraT) throws Exception { 
		 PreparedStatement pStmt=null;
		 ResultSet rs=null;
		 try {
			 String sql = "select * from tb_compra where  id_compra=?"; 
			 pStmt = con.prepareStatement(sql);
			 pStmt.setObject(1, tb_compraT.getId_compra());
			 rs = pStmt.executeQuery();
 			 return resultSetToObjectTransfer(rs); 
 		 } catch (Exception e) {
 			  throw e;
 		} finally {
 			 try { rs.close();
 			 pStmt.close();} catch (Exception e) {}
 			  
 		} 
 	} 

 	 public List<Tb_compraT> getById_compra(Tb_compraT tb_compraT) throws Exception { 
		 PreparedStatement pStmt=null;
		 ResultSet rs=null;
		 try {
			 String sql = "select * from tb_compra where  id_compra = ? "; 
			 pStmt = con.prepareStatement(sql);
			 pStmt.setObject(1, tb_compraT.getId_compra());
			 rs = pStmt.executeQuery();
 			 return resultSetToObjectTransfer(rs); 
 		 } catch (Exception e) {
 			  throw e;
 		} finally {
 			 try { rs.close();
 			 pStmt.close();} catch (Exception e) {}
 			  
 		} 
 	} 

 	 public List<Tb_compraT> getById_fornecedor(Tb_compraT tb_compraT) throws Exception { 
		 PreparedStatement pStmt=null;
		 ResultSet rs=null;
		 try {
			 String sql = "select * from tb_compra where  id_fornecedor = ? "; 
			 pStmt = con.prepareStatement(sql);
			 pStmt.setObject(1, tb_compraT.getId_fornecedor());
			 rs = pStmt.executeQuery();
 			 return resultSetToObjectTransfer(rs); 
 		 } catch (Exception e) {
 			  throw e;
 		} finally {
 			 try { rs.close();
 			 pStmt.close();} catch (Exception e) {}
 			  
 		} 
 	} 

 	 public List<Tb_compraT> getById_forma_pagamento(Tb_compraT tb_compraT) throws Exception { 
		 PreparedStatement pStmt=null;
		 ResultSet rs=null;
		 try {
			 String sql = "select * from tb_compra where  id_forma_pagamento = ? "; 
			 pStmt = con.prepareStatement(sql);
			 pStmt.setObject(1, tb_compraT.getId_forma_pagamento());
			 rs = pStmt.executeQuery();
 			 return resultSetToObjectTransfer(rs); 
 		 } catch (Exception e) {
 			  throw e;
 		} finally {
 			 try { rs.close();
 			 pStmt.close();} catch (Exception e) {}
 			  
 		} 
 	} 

 	 public List<Tb_compraT> getByData_compra(Tb_compraT tb_compraT) throws Exception { 
		 PreparedStatement pStmt=null;
		 ResultSet rs=null;
		 try {
			 String sql = "select * from tb_compra where  data_compra = ? "; 
			 pStmt = con.prepareStatement(sql);
		 java.sql.Date dt1= new java.sql.Date(tb_compraT.getData_compra().getTime());
			 pStmt.setObject(1,dt1);
			 rs = pStmt.executeQuery();
 			 return resultSetToObjectTransfer(rs); 
 		 } catch (Exception e) {
 			  throw e;
 		} finally {
 			 try { rs.close();
 			 pStmt.close();} catch (Exception e) {}
 			  
 		} 
 	} 

 	 public List<Tb_compraT> getByTipo_desconto(Tb_compraT tb_compraT) throws Exception { 
		 PreparedStatement pStmt=null;
		 ResultSet rs=null;
		 try {
			 String sql = "select * from tb_compra where  tipo_desconto = ? "; 
			 pStmt = con.prepareStatement(sql);
			 pStmt.setObject(1, tb_compraT.getTipo_desconto());
			 rs = pStmt.executeQuery();
 			 return resultSetToObjectTransfer(rs); 
 		 } catch (Exception e) {
 			  throw e;
 		} finally {
 			 try { rs.close();
 			 pStmt.close();} catch (Exception e) {}
 			  
 		} 
 	} 

 	 public List<Tb_compraT> getByDesconto(Tb_compraT tb_compraT) throws Exception { 
		 PreparedStatement pStmt=null;
		 ResultSet rs=null;
		 try {
			 String sql = "select * from tb_compra where  desconto = ? "; 
			 pStmt = con.prepareStatement(sql);
			 pStmt.setObject(1, tb_compraT.getDesconto());
			 rs = pStmt.executeQuery();
 			 return resultSetToObjectTransfer(rs); 
 		 } catch (Exception e) {
 			  throw e;
 		} finally {
 			 try { rs.close();
 			 pStmt.close();} catch (Exception e) {}
 			  
 		} 
 	} 

 

 }